<template>
    <view class="flex items-center rounded-full overflow-hidden" v-if="['wait_sub', 'wait_td',
        'wait_sign'].includes(apply_status)">
        <l-button class="flex-1 " size="medium" type="error" @tap="refuseShow = true">拒绝</l-button>
        <l-button class="flex-1 " size="medium" type="primary" @tap="confirm">{{ getStatusText(apply_status)
            }}</l-button>
    </view>
    <u-modal :show="refuseShow" @cancel="refuseShow = false" :title-style="{ color: 'red' }" title="拒绝申请"
        @confirm="refuse" show-cancel-button>
        <view>
            <view class="text-sm text-gray-400 text-center mb-3">请输入原因</view>
            <l-input type="textarea" v-model="refuseForm.remark"></l-input>
        </view>
    </u-modal>
    <u-modal :show="successShow" @cancel="successShow = false" title="同意申请" @confirm="agreeWith" show-cancel-button>
        <view>
            <l-input label="批准金额" suffix="万元" type="digit" v-model="agreeWithForm.approved_amount"></l-input>
            <l-input label="手续费" suffix="元" type="digit" v-model="agreeWithForm.fee" class="mt-3"></l-input>
            <l-select label="放贷银行" class="mt-3" v-model="agreeWithForm.lending_bank"
                :list="settings.dict_partner_banks"></l-select>
        </view>
    </u-modal>
    <u-modal :show="confirmNext" @cancel="confirmNext = false" title="温馨提示"
        @confirm="() => { agreeWith(); confirmNext = false }" show-cancel-button>
        <view>
            确认{{ getStatusText(apply_status) }}？
        </view>
    </u-modal>
</template>
<script setup lang="ts">
import { reactive, ref } from 'vue';
import Order from '@/stores/order'
import Settings from "@/stores/settings"
let settings = Settings();
const props = defineProps<{ is_ysf: number, id: number, apply_status: string }>(), order = Order(), confirmNext = ref(false),
    emit = defineEmits(['update'])

const text: { [key: string]: string } = {
    wait_sub: '提交资料',
    wait_td: '已勘验',
    wait_sign: '受理签约'
};
const refuseShow = ref(false), successShow = ref(false), refuseForm = reactive({ remark: '' }), agreeWithForm = reactive({ approved_amount: '', fee: '', lending_bank: '' });
const agreeWith = async () => {

    await order.do_next(props.id, { ...agreeWithForm, is_ysf: props.is_ysf })
    successShow.value = true
    emit('update')
}
const refuse = async () => {
    await order.reject(props.id, { ...refuseForm, is_ysf: props.is_ysf })
    refuseShow.value = false
    emit('update')
}
const getStatusText = (apply_status: string) => {

    return text[apply_status];
}
const confirm = () => {
    if (props.apply_status == 'wait_sign') {
        successShow.value = true
    } else {
        confirmNext.value = true
    }
}
</script>